package com.web.biz.userbuy.service;
import java.util.ArrayList;
import java.util.List;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.adapter.newsword.pagequery.IPageQueryDAO;
import com.css.sword.platform.web.adapter.newsword.utils.PageQueryUtils;
import com.css.sword.platform.web.dm2mc.Dm2mc;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.web.common.dataDictionary.DE.DE_ALIPAYID;
import com.web.common.dataDictionary.DE.DE_C_BRAND_NEWNUM;
import com.web.common.dataDictionary.DE.DE_C_BRAND_TOTALWEALTH;
import com.web.common.dataDictionary.DE.DE_GMSJ;
import com.web.common.dataDictionary.DE.DE_GMZT;
import com.web.common.dataDictionary.DE.DE_GOODSCATEGORYID;
import com.web.common.dataDictionary.DE.DE_GOODSID;
import com.web.common.dataDictionary.DE.DE_ORDERNO;
import com.web.common.dataDictionary.DE.DE_VIPSYTS;
import com.web.common.dataDictionary.DE.DE_WEBUSERNAME;
import com.web.common.dataDictionary.PO.TCategoryShopPO;
import com.web.common.dataDictionary.PO.TabUserPO;
import com.web.common.dataDictionary.PO.TabUserbuyPO;
import com.web.common.utils.CacheUtil;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class UserbuyService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(UserbuyService.class);
	private static String curpath = "pages/biz/userbuy/";
	public UserbuyService() throws Exception {
		super();
	}


	/**
	 * 初始化我的购买列表
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Service(serviceName = "UserbuyService_initList")
	public IResData initList(IReqData req) throws Exception {
		
		log.debug("开始进入 UserbuyService_initList 方法。");
		IPageQueryDAO pdao=PageQueryUtils.getPageQueryDAO(); 
		IResData res = new SwordRes();
		SQLParam param = new SQLParam();
		param.addParam(LoginInfoUtil.getLoginUserName());
		List<Dm2mc> dmMc = new ArrayList<Dm2mc>();
		List list = pdao.pageQueryByKey("grid_userbuy", "UserbuyService_queryListByYhm", param);
		Dm2mc goodsdm2mc = new Dm2mc("goodsid", "DM_GOODS", "CAPTION");
		Dm2mc goodscategorydm2mc = new Dm2mc("goodscategoryid", "DM_GOODSCATEGORY", "CAPTION");
		dmMc.add(goodsdm2mc);
		dmMc.add(goodscategorydm2mc);
		res.addTable("grid_userbuy", list ,dmMc);
		res.addPage(curpath+"userbuyList.jsp");
		return res;
	}
	
	
	/**
	 * 初始化购买页面
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "UserbuyService_initVipForm")
	public IResData initVipForm(IReqData req) throws Exception {

		log.debug("开始进入 UserbuyService_initVipForm 方法。");

		IResData res = new SwordRes();
		List viplist = CacheUtil.getPullData("DM_GOODS_VIP");
		List newlist = CacheUtil.getPullData("DM_GOODS_NEW");
		List wealthlist = CacheUtil.getPullData("DM_GOODS_WEALTH");
		res.addListWithDataName("vip", viplist);
		res.addListWithDataName("new", newlist);
		res.addListWithDataName("wealth", wealthlist);
		
		res.addPage(curpath+"vipbuyForm.jsp");
		return res;
	}
	

	/**
	 * 支付成功后业务处理方法
	 * @param extra_common_param
	 * @param trade_no
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "UserbuyService_buy")
	public IResData buy(String out_trade_no,String extra_common_param,String trade_no) throws Exception {

		log.debug("开始进入 UserbuyService_buy 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		IResData res = new SwordRes();
		String yhm = LoginInfoUtil.getLoginUserName();
		String flag = "false";
		try {
			//商户订单号
			String orderNo = StringUtil.parse2String(out_trade_no);
			String[] split = extra_common_param.split("\\^");
			//商品id
			String goodsid = StringUtil.parse2String(split[0]);
			//商品分类id
			String categoryid = StringUtil.parse2String(split[1]);
			//真正要处理的业务数据
			String realVal = StringUtil.parse2String(split[2]);
			String[] realValSplit = realVal.split("#");
			//财富值
			int wealthVal = Integer.parseInt(realValSplit[0]);
			//vip值
			int vipVal = Integer.parseInt(realValSplit[1]);
			//new值
			int newVal = Integer.parseInt(realValSplit[2]);
			//保存用户购买表开始
			TabUserbuyPO userbuyPO = new TabUserbuyPO();
			userbuyPO.setOrderno(new DE_ORDERNO(orderNo));
			userbuyPO.setWebusername(new DE_WEBUSERNAME(yhm));
			userbuyPO.setGoodsid(new DE_GOODSID(goodsid));
			userbuyPO.setGoodscategoryid(new DE_GOODSCATEGORYID(categoryid));
			userbuyPO.setGmsj(new DE_GMSJ(DateFormatUtil.getNowDate()));
			userbuyPO.setGmzt(new DE_GMZT("success"));
			//保存支付宝交易号
			userbuyPO.setAlipayid(new DE_ALIPAYID(trade_no));
			dao.insertPO(userbuyPO);
			//保存用户购买表结束
			//修改web用户vip和财富信息开始
			SQLParam param = new SQLParam();
			param.addParam(yhm);
			if (wealthVal > 0 || newVal > 0) {
				TCategoryShopPO shopPO = dao.queryPOByKey("grxx_queryShopxxByYhm", param, TCategoryShopPO.class);
				shopPO.setCBrandTotalwealth(new DE_C_BRAND_TOTALWEALTH(shopPO.getCBrandTotalwealth().value+wealthVal));
				shopPO.setCBrandNewnum(new DE_C_BRAND_NEWNUM(shopPO.getCBrandNewnum().value+newVal));
				dao.updatePO(shopPO);
			}
			if (vipVal > 0) {
				TabUserPO userPO = dao.queryPOByKey("LoginRegisterService_queryByYhm", param, TabUserPO.class);
				userPO.setVipsyts(new DE_VIPSYTS(userPO.getVipsyts().value+vipVal));
				dao.updatePO(userPO);
			}
			//修改web用户vip和财富信息结束
			dao.commitAll();
			flag = "true";
		} catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
		}
		
		res.put("flag", flag);
		return res;
	}

}